package com.baizhi.service.impl;

import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Transactional
public class UserServiceimpl implements UserService {
    Logger logger = LoggerFactory.getLogger(UserServiceimpl.class);
    @Resource
    private UserDao userDao;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<User> selectAll(Integer page, Integer limit, String userTelphone) {
        logger.debug("创建计数器");
        PageHelper.startPage(page,limit);
        PageInfo<User> pageInfo = new PageInfo<>(userDao.selectAll(userTelphone));
        return pageInfo;
    }

    @Override
    public void updateStatus(Integer userStatus, Integer userId) {
        logger.debug("创建计数器");
        userDao.updateStatus(userStatus,userId);
    }

    @Override
    public List<Integer> selectNum() {
        logger.debug("创建计数器");

        return userDao.selectNum();
    }

    @Override
    public List<Integer> selectWeek() {
        logger.debug("创建计数器");
        return userDao.selectWeek();
    }

    @Override
    public List<Map> selectByProvince() {
        logger.debug("创建计数器");
        return userDao.selectByProvince();
    }

    @Override
    public Map selectByThread() throws InterruptedException {
        logger.debug("创建计数器");
        logger.info("创建计数器");
        logger.warn("创建计数器");
        logger.error("创建计数器");
        Map map = new HashMap();
//        1.创建计数器  参数为任务数量
        CountDownLatch countDownLatch = new CountDownLatch(3);

//        2.创建子线程执行三个任务
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                注册量查询
                List<Integer> integers = userDao.selectWeek();
                map.put("count",integers);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                男女人数
                List<Integer> list = userDao.selectNum();
                map.put("sex",list);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                地区分布
                List<Map> maps = selectByProvince();
                map.put("province",maps);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });
//        3.通过计数器让主线程等待
        countDownLatch.await();

//        4.return
        return map;
    }

    @Override
    public Map selectAllCount() {
        logger.debug("创建计数器");
        Map map = new HashMap<>();
//        注册量
        List<Integer> integers = userDao.selectWeek();
//        地区分布
        List<Map> maps = selectByProvince();
//        男女比例
        List<Integer> list = userDao.selectNum();
        map.put("sex",list);
        map.put("count",integers);
        map.put("province",maps);
        return map;
    }


}
